package com.mflex.mwms.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mflex.mwms.BuildConfig;
import com.mflex.mwms.R;
import com.mflex.mwms.manager.TokenManager;
import com.mflex.mwms.model.CustomApiResult;
import com.mflex.mwms.model.DeviceLoginRequest;
import com.mflex.mwms.model.DeviceLoginReturn;
import com.mflex.mwms.utils.MessageUtils;
import com.mflex.mwms.utils.XToastUtils;
import com.xuexiang.xhttp2.XHttp;
import com.xuexiang.xhttp2.callback.CallBackProxy;
import com.xuexiang.xhttp2.callback.SimpleCallBack;
import com.xuexiang.xhttp2.exception.ApiException;
import com.xuexiang.xui.utils.WidgetUtils;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xutil.app.AppUtils;
import com.xuexiang.xutil.common.StringUtils;
import com.xuexiang.xutil.net.JsonUtil;
import com.xuexiang.xutil.system.DeviceUtils;

import java.util.ArrayList;

public class LoginActivity extends BaseActivity {

    private TextView txtTtitle;
    private LinearLayout loginLogin;

    private EditText txtUser;

    private EditText txtPwd;
    private Button btnLogin;

    private TextView txtVersion;
    private TextView txtApi;

    MiniLoadingDialog mMiniLoadingDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mMiniLoadingDialog = WidgetUtils.getMiniLoadingDialog(this, "登录中...");


        txtTtitle = (TextView) findViewById(R.id.txtTtitle);
//        loginLogin = (LinearLayout) findViewById(R.id.loginLogin);

        txtUser = (EditText) findViewById(R.id.txtUser);

        txtPwd = (EditText) findViewById(R.id.txtPwd);
        btnLogin = (Button) findViewById(R.id.btnLogin);

        txtVersion = (TextView) findViewById(R.id.txtVersion);
        txtApi = (TextView) findViewById(R.id.txtApi);


//        //隐藏配置项
//        loginLogin.setVisibility(View.VISIBLE);


        //设置焦点
        txtUser.setFocusable(true);
        txtUser.setFocusableInTouchMode(true);
        txtUser.requestFocus();

        if(StringUtils.equals(BuildConfig.FLAVOR,"UAT")){
            txtTtitle.setText(AppUtils.getAppName() + "测试");
            txtTtitle.setTextColor(Color.RED);
        }else {
            txtTtitle.setText(AppUtils.getAppName());
        }

        txtVersion.setText("程序版本" + AppUtils.getAppVersionName());
        txtApi.setText(BuildConfig.API_HOST);


        //设置监听
        setListener();

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void setListener() {
        //登录按钮监听
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (StringUtils.isEmpty(txtUser.getText().toString())) {
                    XToastUtils.error("MES账号为空!", 2000);
                    return;
                }
                if (StringUtils.isEmpty(txtPwd.getText().toString())) {
                    XToastUtils.error("MES密码为空!", 2000);
                    return;
                }
                request(v);
            }
        });
    }

    /**
     * 登录请求
     */
    private void request(View v) {
        mMiniLoadingDialog.show();

        DeviceLoginRequest user = new DeviceLoginRequest();
        user.setUserName(txtUser.getText().toString());
        user.setPassword(txtPwd.getText().toString());
        user.setDeviceName(DeviceUtils.getMacAddress());
        XHttp.post("api/User/DeviceLogin")
                .upJson(JsonUtil.toJson(user))
                .syncRequest(false)
                .onMainThread(true)
                .timeOut(120000)
                .execute(new CallBackProxy<CustomApiResult<DeviceLoginReturn>, DeviceLoginReturn>(new SimpleCallBack<DeviceLoginReturn>() {

                    @Override
                    public void onSuccess(DeviceLoginReturn response) throws Throwable {
                        mMiniLoadingDialog.dismiss();
                        TokenManager.getInstance()
                                .setToken(response.getApitoken())
                                .setLoginUser(user);
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putStringArrayListExtra("Warehoses", (ArrayList<String>) response.getWarehouse());
                        startActivity(intent);
                    }

                    @Override
                    public void onError(ApiException e) {
                        mMiniLoadingDialog.dismiss();

//                        XToastUtils.error(e.getMessage(),3000);
//                        new MaterialDialog.Builder(v.getContext())
//                                .iconRes(R.drawable.icon_warning)
//                                .title(R.string.tip_warning)
//                                .content(e.getMessage())
//                                .positiveText(R.string.lab_submit)
//                                .show();
                        MessageUtils.errorMsg(LoginActivity.this,e);
                    }
                }) {
                });
    }

    @Override
    public void onClick(View view) {

    }

    @Override
    public boolean onKey(View view, int i, KeyEvent keyEvent) {
        return false;
    }
}
